package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import com.gljx.web.Entity.Student;
import com.gljx.web.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.ReturnPremium;
import com.gljx.web.Entity.ReturnPremiumEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.ReturnPremiumEntryMapper;
import com.gljx.web.dao.ReturnPremiumMapper;
import com.gljx.web.service.ReturnPremiumService;

@Service
@Transactional
public class ReturnPremiumServiceImpl extends CommonDao implements ReturnPremiumService{
	@Autowired
	private StudentService studentService;

	@Autowired
	private ReturnPremiumEntryMapper returnPremiumEntryMapper;
	
	@Autowired
	private ReturnPremiumMapper returnPremiumMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.ReturnPremiumMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.ReturnPremiumMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 查询退款登记子类信息
	 */
	@Override
	public List<ReturnPremiumEntry> queryReturnPremiumEntry(String fparentId, String studentName) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", fparentId);
		paramMap.put("v_studentName", studentName);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.ReturnPremiumEntryMapper.selectReturnPremiumEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<ReturnPremiumEntry> returnPremiumEntrys = (List<ReturnPremiumEntry>) paramMap.get("v_cursor");
		return returnPremiumEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editReturnPremium(ReturnPremium returnPremium, String[] fseq, String[] cfstudentno,
			String[] cfstudentid, String[] cfidcardaddress, String[] cfsqzjcxdh, String[] cfreceivable,
			String[] cfreceived, String[] cfprivilege, String[] cfarrearage, String[] cfexpenditure,
			String[] cfythj, String[] cfcash, String[] cfcard, String[] cfyhzz,
			String[] cfamount, String[] cfdesc, User user,String time) {
		// TODO Auto-generated method stub
		if("".equals(returnPremium.getFid()) || returnPremium.getFid() == null){
			//创建人id
			returnPremium.setFcreatorid(user.getFid());
			//利用UUID生成fid
			returnPremium.setFid(GetFid.UUIDIntoFid());
			//当前时间
			returnPremium.setFcreatetime(time);

			//状态
            returnPremium.setCfbillstate(3L);
            //添加父类信息
			returnPremiumMapper.insertSelective(returnPremium);
			if(fseq == null || fseq.length == 0){
				
			}else{

				Student student=new Student();
				//将学员状态改变为已退学已退款--23
				for (int i=0;i<cfstudentid.length;i++){
					student.setFid(cfstudentid[i]);
					student.setCfstudentstate((long) 23);
					studentService.updateStudent(student);
				}
				//添加子类信息
				returnPremiumEntry(fseq,cfstudentno,cfstudentid,cfidcardaddress,cfsqzjcxdh,cfreceivable,cfreceived,cfprivilege,
						cfarrearage,cfexpenditure,cfythj,cfcash,cfcard,cfyhzz,cfamount,cfdesc,returnPremium.getFid());
			}
		}else{
			//修改时间时间
			returnPremium.setFlastupdatetime(time);
			//修改人id
			returnPremium.setFlastupdateuserid(user.getFid());
			//修改父类信息
			returnPremiumMapper.updateByPrimaryKeySelective(returnPremium);
			//先根据父类id删除
			returnPremiumEntryMapper.deleteByFparentId(returnPremium.getFid());
			//添加
			if(fseq == null || fseq.length == 0){
				
			}else{
				//添加子类信息
				returnPremiumEntry(fseq,cfstudentno,cfstudentid,cfidcardaddress,cfsqzjcxdh,cfreceivable,cfreceived,cfprivilege,
						cfarrearage,cfexpenditure,cfythj,cfcash,cfcard,cfyhzz,cfamount,cfdesc,returnPremium.getFid());
			}
		}
	}
	
	/**
	 * 添加或修改方法
	 * @param fseq
	 * @param cfstudentno
	 * @param cfstudentid
	 * @param cfidcardaddress
	 * @param cfsqzjcxdh
	 * @param cfreceivable
	 * @param cfreceived
	 * @param cfprivilege
	 * @param cfarrearage
	 * @param cfexpenditure
	 * @param cfythj
	 * @param cfcash
	 * @param cfcard
	 * @param cfyhzz
	 * @param cfamount
	 * @param cfdesc
	 * @param fparentId
	 */
	public void returnPremiumEntry(String[] fseq, String[] cfstudentno,String[] cfstudentid, String[] cfidcardaddress, String[] cfsqzjcxdh,
			String[] cfreceivable,String[] cfreceived, String[] cfprivilege, String[] cfarrearage, String[] cfexpenditure,
			String[] cfythj, String[] cfcash, String[] cfcard, String[] cfyhzz,String[] cfamount, String[] cfdesc,String fparentId){
			for (int i = 0; i < fseq.length; i++) {
				ReturnPremiumEntry returnPremiumEntry = new ReturnPremiumEntry();
				returnPremiumEntry.setCfstudentno(cfstudentno[i]);
				returnPremiumEntry.setCfstudentid(cfstudentid[i]);
				returnPremiumEntry.setCfidcardaddress(cfidcardaddress[i]);
				returnPremiumEntry.setCfsqzjcxdh(cfsqzjcxdh[i]);
				if("".equals(cfreceivable[i])){
					
				}else{
					returnPremiumEntry.setCfreceivable(BigDecimalFormat.formot(cfreceivable[i]));
				}
				if("".equals(cfreceived[i])){
					
				}else{
					returnPremiumEntry.setCfreceived(BigDecimalFormat.formot(cfreceived[i]));
				}
				if("".equals(cfprivilege[i])){
					
				}else{
					returnPremiumEntry.setCfprivilege(BigDecimalFormat.formot(cfprivilege[i]));
				}
				if("".equals(cfarrearage[i])){
					
				}else{
					returnPremiumEntry.setCfarrearage(BigDecimalFormat.formot(cfarrearage[i]));
				}
				if("".equals(cfexpenditure[i])){
					
				}else{
					returnPremiumEntry.setCfexpenditure(BigDecimalFormat.formot(cfexpenditure[i]));
				}
				if("".equals(cfythj[i])){
					
				}else{
					returnPremiumEntry.setCfythj(BigDecimalFormat.formot(cfythj[i]));
				}
				if("".equals(cfcash[i])){
					
				}else{
					returnPremiumEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
				}
				if("".equals(cfcard[i])){
					
				}else{
					returnPremiumEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
				}
				if("".equals(cfyhzz[i])){
					
				}else{
					returnPremiumEntry.setCfyhzz(BigDecimalFormat.formot(cfyhzz[i]));
				}
				if("".equals(cfamount[i])){
					
				}else{
					returnPremiumEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				returnPremiumEntry.setCfdesc(cfdesc[i]);
				returnPremiumEntry.setFid(GetFid.UUIDIntoFid());
				returnPremiumEntry.setFparentid(fparentId);
				returnPremiumEntry.setFseq((long)i+1);
				returnPremiumEntryMapper.insertSelective(returnPremiumEntry);
			}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public ReturnPremium queryReturnPremiumByFid(String fid) {
		// TODO Auto-generated method stub
		return returnPremiumMapper.selectReturnPremiumByFid(fid);
	}

	/**
	 * 实现删除
	 */
	@Override
	public void deleteReturnPremium(String fid, String[] deleteId) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//删除子类信息
				returnPremiumEntryMapper.deleteByFparentId(deleteId[i]);
				//删除父类信息
				returnPremiumMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//删除子类信息
			returnPremiumEntryMapper.deleteByFparentId(fid);
			//删除父类信息
			returnPremiumMapper.deleteByPrimaryKey(fid);
		}
		
	}

	/**
	 * 实现审核，反审核
	 */
	@Override
    public int auditReturnPremium(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
        Axamine axamine = new Axamine();
        ReturnPremium returnPremium = new ReturnPremium();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                //fid
                returnPremium.setFid(fid);
                //修改人
                returnPremium.setFlastupdateuserid(fhandlerId);
                //审核人
                returnPremium.setFauditorid(fhandlerId);
                //状态
                returnPremium.setCfbillstate(5L);
                //结束
                return returnPremiumMapper.updateByPrimaryKeySelective(returnPremium);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    //fid
                    returnPremium.setFid(fid);
                    returnPremium.setCfbillstate(3L);
                    returnPremiumMapper.updateByPrimaryKeySelective(returnPremium);
                }
                return 2;
            } else {
                //错误
                return 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            //fid
            returnPremium.setFid(fid);
            //修改人
            returnPremium.setFlastupdateuserid(fhandlerId);
            //审核人
            returnPremium.setFauditorid(fhandlerId);
            //状态
            returnPremium.setCfbillstate(3L);
            return returnPremiumMapper.updateByPrimaryKeySelective(returnPremium);
        }
    }

    @Override
    public int modifyReturnPremium(ReturnPremium returnPremium) {
        return returnPremiumMapper.updateByPrimaryKeySelective(returnPremium);
    }

    @Override
    public List<ReturnPremiumEntry> selectReturnPremiumByInfo(String fcompanyid, String studentName, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return returnPremiumEntryMapper.selectReturnPremiumByInfo(map);
    }
}
